<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Comment_model extends CI_Model {
	function add($data) {
		$this->db->trans_start();
		$this->db->insert('comment', $data);
		$insert_id = $this->db->insert_id();
		$this->db->trans_complete();
		log_message('debug', $this->db->last_query());
		
		return $insert_id;
	}
	
	function exists_id($comment_id) {
		$this->db->where('id', $comment_id);
		$this->db->where('status', DB_STATUS_NORMAL);
		$query = $this->db->get('comment');
		log_message('debug', $this->db->last_query());
		
		return $this->db->count_all_results() >= 1;
	}
	
	function get_by_id($comment_id) {
		$this->db->where('comment_id', $comment_id);
		$query = $this->db->get('vw_comment');
		log_message('debug', $this->db->last_query());
		
		$count = $this->db->count_all_results();
		return $count == 1 ? $query->row_array() : NULL;
	}
	
	function belongs_to_user($comment_id, $user_id) {
		if ($comment_id <=0 || $user_id <= 0) return FALSE;
		$this->db->where('id', $comment_id);
		$this->db->where('user_id', $user_id);
		$this->db->where('status', DB_STATUS_NORMAL);
		$this->db->from('comment');
		$count = $this->db->count_all_results();
		log_message('debug', $this->db->last_query());
		
		return $count >= 1;
	}
	
	function get_all_list_by_post($post_id) {
		$this->db->where('post_id', $post_id);
		$query = $this->db->get('vw_comment');
		log_message('debug', $this->db->last_query());
		
		return $query->result_array();
	}
	
	function inc_good_count($comment_id, $user_id) {
		$this->db->trans_start();
		$this->db->insert('user_good_comment', array(
			'user_id' => $user_id,
			'comment_id' => $comment_id,
			'status' => DB_STATUS_NORMAL,
			'create_time' => date('Y-m-d H:i:s')
		));
		log_message('debug', $this->db->last_query());
		$this->db->where('id', $comment_id);
		$this->db->set('good_count', 'good_count+1', FALSE);
		$this->db->update('comment');
		$affected_rows = $this->db->affected_rows();
		$this->db->trans_complete();
		log_message('debug', $this->db->last_query());
		
		return $affected_rows;
	}
	
	function is_gooded($comment_id, $user_id) {
		$this->db->where('comment_id', $comment_id);
		$this->db->where('user_id', $user_id);
		$this->db->where('status', DB_STATUS_NORMAL);
		$this->db->from('user_good_comment');
		log_message('debug', $this->db->last_query());
		
		return $this->db->count_all_results() >= 1;
	}
	
	function delete($comment_id) {
		$this->db->trans_start();
		$this->db->where('id', $comment_id);
		$this->db->set('status', DB_STATUS_DELETED);
		$this->db->update('comment');
		$affected_rows = $this->db->affected_rows();
		$this->db->trans_complete();
		log_message('debug', $this->db->last_query());
		
		return $affected_rows;
	}
	/*
	function get_all_list_all_count_by_post($post_id) {
		$this->db->where('status', DB_STATUS_NORMAL);
		$this->db->where('post_id', $post_id);
		$this->db->from('comment');
		$all_count = $this->db->count_all_results();
		log_message('debug', $this->db->last_query());
		
		return $all_count;
	}
	*/
}